﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_StaffDict_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim _curHuyenId As Integer = Session("HuyenId")
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                Using _data As New VSEntities
                    Dim p = (From h In _data.Provinces Where h.Id = _curHuyenId Select h.Id, h.Title).ToList
                    If Not p Is Nothing Then
                        With ddlHuyen
                            .DataSource = p
                            .DataValueField = "Id"
                            .DataTextField = "Title"
                            .DataBind()
                        End With
                    End If

                End Using

                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If

    End Sub
    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim _curHuyenId As Integer = Session("HuyenId")
        Dim p As StaffDict = (From q In data.StaffDicts Where q.ProvinceId = _curHuyenId And q.Id = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then

            txtCode.Text = IIf(IsNothing(p.Code) = True, "", p.Code)
            txtName.Text = IIf(IsNothing(p.Name) = True, "", p.Name)
            txtDescription.Text = IIf(IsNothing(p.Desription) = True, "", p.Desription)
        End If
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As New VSEntities
        Dim _curHuyenId As Integer = Session("HuyenId")
        Dim p As StaffDict = (From q In data.StaffDicts Where q.ProvinceId = _curHuyenId And q.Id = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            'DUC 10-09-2014 kiem tra Code va Name da ton tai chua loai chinh no khi sua
            Dim checkCode = (From q In data.StaffDicts Where q.ProvinceId = _curHuyenId And q.Id <> hidID.Value And q.Code = txtCode.Text.Trim() Select q.Code).FirstOrDefault
            If checkCode Is Nothing Then
                Dim checkName = (From q In data.StaffDicts Where q.ProvinceId = _curHuyenId And q.Id <> hidID.Value And q.Name = txtName.Text.Trim() Select q).FirstOrDefault
                If checkName Is Nothing Then
                    Try
                        p.Id = hidID.Value
                        p.Code = txtCode.Text.Trim
                        p.Name = txtName.Text.Trim()
                        p.ProvinceId = _curHuyenId
                        p.Desription = txtDescription.Text
                        p.Modified = Date.Now
                        p.Modifier = Session("UserName")
                        Dim t = (From h In data.StaffDicts Order By h.Sort Descending Select h).FirstOrDefault
                        If Not t Is Nothing Then
                            p.Sort = CInt(t.Sort.ToString) + 1
                        Else
                            p.Sort = 1
                        End If
                        data.SaveChanges()
                        Insert_App_Log("Update StaffDict:" & txtName.Text.Trim & "", Function_Name.StaffDict, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                        Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/StaffDict/List.aspx?Id=" & Request("Id") & "';", Me.Page, True)
                        'Response.Redirect("List.aspx?Id=" + drlParent.SelectedValue.ToString)
                    Catch ex As Exception
                        log4net.Config.XmlConfigurator.Configure()
                        log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                        Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                    End Try
                Else
                    Excute_Javascript("Alertbox('Tên cán bộ tồn tại trong hệ thống.')", Me.Page, True)
                End If
            Else
                Excute_Javascript("Alertbox('Mã đã tồn tại trong hệ thống.')", Me.Page, True)
            End If
        End If
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
